package com.gohome.gohomechat.view.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.gohome.gohomechat.utils.ToastUtil;

/**
 * Created by dllo on 16/2/29.
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected static final String TAG=BaseActivity.class.getSimpleName();
    protected String tag=getClass().getSimpleName();
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ActivityControl.addAty(tag, this);

        dialog = new ProgressDialog(this);
        dialog.setTitle("请稍后");
        dialog.setMessage("数据加载中...");

        initPresenter();
        initView();
        initData();
    }

    /**
     * 简化findViewById
     * @param id
     * @param <T>
     * @return
     */
    protected <T extends View> T findView(int id){
        T t = (T) findViewById(id);
        return t;
    }

    /**
     * 返回布局文件的地址
     * @return
     */
    protected abstract int getLayout();

    /**
     * 初始化Presenter
     */
    protected abstract void initPresenter();

    /**
     * 初始化控件
     */
    protected abstract void initView();

    /**
     * 数据的操作
     */
    protected abstract void initData();

    /**
     * 显示dialog
     */
    protected void showProgressDialog(){
        dialog.show();
    }

    /**
     * 隐藏dialog
     */
    protected void dismissProgressDialog(){
        dialog.dismiss();
    }

    /**
     *
     */
    protected void toastFailed(){
        ToastUtil.showToast("数据加载失败*_*!");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityControl.removeAty(tag);
    }
}
